@echo off

cls

set COMPILE=
set CLEAN=
set EXECUTE=

call %PRJ_DIR%\master_bat\config.bat

if "%1"=="debug" (
	echo *** build with debug mode ***
	set COMPILE=1
	set CLEAN=
) else if "%1"=="release" (
	echo *** build with release mode ***
	set COMPILE=2
	set CLEAN=
) else if "%1"=="data" (
	echo *** build with release mode ***
	set COMPILE=3
	set CLEAN=
) else if "%1"=="run" (
	set COMPILE=
	set EXECUTE=1
	set CLEAN=
) else if "%1"=="clean" (
	set COMPILE=
	set EXECUTE=
	set CLEAN=1
) else (
	echo make paremeter
	echo   parameter is one of
	echo      debug   : compile app with _DEBUG defined, no obfuscation
	echo      release : compile and obfuscate
	echo      run     : execute last compiled version
	echo      clean   : clean all build directories
	goto error
)	
	
if "%CLEAN%"=="1" (
	echo *** clean all build and release directory ***
	if exist %RELEASE_DIR% (rd /s /q %RELEASE_DIR%)
	if exist %VERSION_DIR% (rd /s /q %VERSION_DIR%)	
	echo *** end clean ***
	goto end
)

if "%COMPILE%"=="" (
	goto run
)

if exist %RELEASE_DIR%\%PRJ_FULL_NAME%.* (del /q /s %RELEASE_DIR%\%PRJ_FULL_NAME%.*)

if not exist %RELEASE_DIR% (MD %RELEASE_DIR%)
if not exist %VERSION_DIR% (MD %VERSION_DIR%)
if not exist %WORKDIRECTORY% (MD %WORKDIRECTORY%)

echo *** Copying data ***

if "%COMPILE%"=="3" (

	if exist %SRC_PATH% (rd /s /q %SRC_PATH%)
	if exist %DATA_PATH% (rd /s /q %DATA_PATH%)

	echo *** Creating working directories ***	
	mkdir %SRC_PATH%
	mkdir %DATA_PATH%
	mkdir %DATA_PATH_RAW%
	mkdir %DATA_TO_PACK%

	mkdir %SRC_PATH_RAW%
	mkdir %SRC_PATH_PREPROCESS%
	mkdir %SRC_PATH_OBFUSCATED%
	mkdir %SRC_PATH_COMPILE%
	mkdir %SRC_PATH_PREVERIFY%

	if exist %MASTER_DATA% (
		echo ... copy from master_data to data\0_data ...
		xcopy /S /Y %MASTER_DATA% %DATA_PATH_RAW%\ > NUL
		
		echo ... build file ...	
		cd %DATA_PATH_RAW%\file
			copy *.* %DATA_TO_PACK%						
		
		echo ... build gfx ...	
		cd %DATA_PATH_RAW%\gfx\00_sprites
			copy *.* %DATA_TO_PACK%				
		cd %DATA_PATH_RAW%\gfx\00_background
			copy *.* %DATA_TO_PACK%		
		
		echo ... build sound ...	
		cd %DATA_PATH_RAW%\sound\background
			copy *.* %DATA_TO_PACK%
		cd %DATA_PATH_RAW%\sound\effects
			copy *.* %DATA_TO_PACK%
			
		echo ... build icon ...	
		cd %DATA_PATH_RAW%\icon
			copy *.png %DATA_TO_PACK%	
	)
	if exist %SPECIFIC_DATA% (
	echo ... copy from data to data\0_data ...
	xcopy /S /Y %SPECIFIC_DATA% %DATA_PATH_RAW%\ > NUL
	
	echo ... build gfx ...	
	cd %DATA_PATH_RAW%\gfx\00_sprites
		copy *.* %DATA_TO_PACK%				
	cd %DATA_PATH_RAW%\gfx\00_background
		copy *.* %DATA_TO_PACK%		
	
	echo ... build sound ...	
	cd %DATA_PATH_RAW%\sound\background
		copy *.* %DATA_TO_PACK%
	cd %DATA_PATH_RAW%\sound\effects
		copy *.* %DATA_TO_PACK%
		
	echo ... build icon ...	
	cd %DATA_PATH_RAW%\icon
		copy *.png %DATA_TO_PACK%	
		
	echo ... buil file txt ...	
	cd %DATA_PATH_RAW%\file
		copy *.* %DATA_TO_PACK%
	)

	
	
	goto end
)

cd %RELEASE_DIR%

:GENERATE_MANIFEST
echo Generating MANIFEST.MF...
echo Manifest-Version: 1.0> MANIFEST.MF
echo MIDlet-Name: %PRJ_MIDLET_NAME%>> MANIFEST.MF
echo MIDlet-Version: %VERSION%>> MANIFEST.MF
echo MIDlet-Vendor: Gameloft SA>> MANIFEST.MF
echo MIDlet-Icon: /icon.png>> MANIFEST.MF
echo MIDlet-1: %PRJ_MIDLET_NAME%, /icon.png, %MIDLET_NAME%>> MANIFEST.MF
echo MicroEdition-Configuration: CLDC-1.0>>MANIFEST.MF
echo MicroEdition-Profile: MIDP-2.0>>MANIFEST.MF
cd..

echo *** Coying sources to raw directory ***

cd %PRJ_DIR%
dir /B/S %MASTER_SRC%\*.java > #src_file.txt
dir /B/S %MASTER_SRC%\*.h >> #src_file.txt


if NOT "%SPECIFIC_SRC%" == "" (
	dir /B/S %SPECIFIC_SRC%\*.h >> #src_file.txt
	dir /B/S %SPECIFIC_SRC%\*.java >> #src_file.txt
)

if %COMPILE%==1 (	
	"%JAVA_BIN%\bin\java.exe" -cp %TOOLS_PATH%\CopyAndAnotate copyAndAnotate -d %SRC_PATH_RAW%\ -f #src_file.txt	
) else (
	for /F %%i in (#src_file.txt) do (
		copy /y %%i %SRC_PATH_RAW% > NUL
	)
)
DEL /q /s #src_file.txt
REM copy /y %SPECIFIC_SRC%\CFont.java %SRC_PATH_PREPROCESS% > NUL
copy /y %MASTER_SRC%\CFont.h %SRC_PATH_RAW% > NUL
copy /y %SPECIFIC_SRC%\CFont.h %SRC_PATH_RAW% > NUL
echo *** Preprocessing ***

echo #define SCR_W  %SCREEN_W% >>%SRC_PATH_RAW%\defines.h
echo #define SCR_H  %SCREEN_H% >>%SRC_PATH_RAW%\defines.h

if "%BUILD_NAME%"=="SonyEricsson_K800i" set DEFINES=%DEFINES% -D SONY_K800i

mkdir %SRC_PATH_PREPROCESS%\tmp

copy %SRC_PATH_RAW%\* %SRC_PATH_PREPROCESS%\tmp > NUL

cd %SRC_PATH_PREPROCESS%\tmp
for %%i in (*.java) do (	
	"%TOOLS_PATH%\cpp\cpp.exe" -E -C -P %DEFINES% %%i %SRC_PATH_PREPROCESS%\%%i
)

echo *** Compiling source files ***
%JAVA_BIN%\bin\javac.exe  -bootclasspath %BUILD_LIB% -encoding UTF8 -target 1.1 -d %SRC_PATH_COMPILE% %SRC_PATH_PREPROCESS%\*.java

echo *** Jaring compiling class ***
%JAVA_BIN%\bin\jar.exe -cvfm %SRC_PATH%\%JAR_NAME%.jar %RELEASE_DIR%\MANIFEST.MF -C %SRC_PATH_COMPILE% .

echo *** Proguard ***
	cd %SRC_PATH%
	if "%COMPILE%"=="2" (
		echo ...obfuscating...
		rem ---- proguard configuration
		echo -libraryjars %BUILD_LIB%		 	> proguard.pro
		echo -injars %JAR_NAME%.jar				>> proguard.pro
		echo -outjar %JAR_NAME%_OBFUSCATE.jar		>> proguard.pro
		echo -keep public class * extends javax.microedition.midlet.MIDlet >> proguard.pro
		echo -overloadaggressively						>> proguard.pro
		echo -verbose >> proguard.pro
		echo -printseeds %JAR_NAME%.seed.txt >> proguard.pro
		echo -printmapping %JAR_NAME%.mapping.txt	>> proguard.pro

		Echo ...[[[ mapping can be found in file %JAR_NAME%.mapping.txt ]]]

		rem ---- obfuscate
	    %JAVA_BIN%\bin\java.exe -jar "%TOOLS_PATH%\Proguard\proguard.jar" @proguard.pro > proguard.log
	    if not %ERRORLEVEL%==0 (
	        echo ERROR:: error while obfuscating
	        goto error
	    )
	    if not exist %JAR_NAME%_OBFUSCATE.jar (
			echo ERROR:: error while obfuscating
			goto error
		)
	    copy /y %JAR_NAME%_OBFUSCATE.jar %SRC_PATH_OBFUSCATED%\%JAR_NAME%.jar > NUL

		cd %SRC_PATH_OBFUSCATED%
		%JAVA_BIN%\bin\jar -xf %JAR_NAME%.jar
		del %JAR_NAME%.jar

	) else if "%COMPILE%"=="1" (
		copy /y %JAR_NAME%.jar %SRC_PATH_OBFUSCATED%\%JAR_NAME%.jar > NUL
		cd %SRC_PATH_OBFUSCATED%
		%JAVA_BIN%\bin\jar -xf %JAR_NAME%.jar
		del %JAR_NAME%.jar
	)

cd %SRC_PATH%

echo *** Preverifying class files ***
%J2ME_DIR%\bin\preverify.exe -classpath %BUILD_LIB% -d %SRC_PATH_PREVERIFY% %SRC_PATH_OBFUSCATED%

echo *** Jaring preverified class ***
%JAVA_BIN%\bin\jar.exe -cvfm %RELEASE_DIR%\%PRJ_FULL_NAME%.jar %RELEASE_DIR%\MANIFEST.MF -C %SRC_PATH_PREVERIFY% . -C %DATA_TO_PACK% .


cd %RELEASE_DIR%

:GENERATE_JAD
echo Generating %PRJ_FULL_NAME%.jad...
echo MIDlet-Name: %PRJ_MIDLET_NAME%> %PRJ_FULL_NAME%.jad
echo MIDlet-Version: %VERSION%>> %PRJ_FULL_NAME%.jad
echo MIDlet-Vendor: Gameloft SA>> %PRJ_FULL_NAME%.jad
echo MIDlet-Icon: /icon.png>> %PRJ_FULL_NAME%.jad
echo MIDlet-1: %PRJ_MIDLET_NAME%, /icon.png, %MIDLET_NAME%>> %PRJ_FULL_NAME%.jad
echo MIDlet-Description: %DESCRIPTION%>> %PRJ_FULL_NAME%.jad
echo MIDlet-Jar-URL: %PRJ_FULL_NAME%.jar>> %PRJ_FULL_NAME%.jad
echo MIDlet-Data-Size: 1024>> %PRJ_FULL_NAME%.jad
cd..

call %TOOLS_PATH%\jarsize.bat %RELEASE_DIR%\%PRJ_FULL_NAME%.jar %RELEASE_DIR%\%PRJ_FULL_NAME%.jad

if exist %RELEASE_DIR%\MANIFEST.MF (DEL /q /s %RELEASE_DIR%\MANIFEST.MF)

:run
if "%EXECUTE%"=="" (
	goto end
)
call %MASTER_BAT%\run.bat

:error
echo build process stopped, an error occured
cd %PRJ_DIR%

:end
cd %PRJ_DIR%


